﻿using System;
using System.Configuration;

namespace Slippor.Douban.Radio.Assist
{
    public class SettingManager
    {
        private readonly ApplicationSettingsBase _settings;

        public SettingManager(ApplicationSettingsBase settings)
        {
            _settings = settings;
        }

        public ApplicationSettingsBase Settings
        {
            get { return _settings; }
        }

        public void SetSetting(String key, object value, Type type, object defaultValue)
        {
            _settings[key] = value;
            _settings.Save();
        }

        public void SetString(String key, String value)
        {
            SetSetting(key, value, typeof (String), String.Empty);
        }

        public void SetBoolean(String key, bool value)
        {
            SetSetting(key, value, typeof (Boolean), false);
        }

        public object GetSetting(String key)
        {
            if (_settings.Properties[key] != null)
            {
                return _settings[key];
            }
            else
            {
                return null;
            }
        }

        public bool GetBoolean(String key)
        {
            bool value = false;
            object result = GetSetting(key);
            if (result != null)
            {
                Boolean.TryParse(result.ToString(), out value);
            }
            return value;
        }

        public String GetString(String key)
        {
            String value = String.Empty;
            object result = GetSetting(key);
            if (result != null)
            {
                value = result.ToString();
            }
            return value;
        }
    }
}